package com.springbootbase.springbootproject.utils;

import com.springbootbase.springbootproject.pojo.Student;
import com.springbootbase.springbootproject.pojo.Teacher;
import eg.model.CompareTask;
import eg.model.shortWord;
import eg.util.TextUtil;
import org.apache.tika.exception.TikaException;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;

import java.io.IOException;
import java.util.*;

public class RulesRecommend {

    private List<Student> students;
    private List<Teacher> teachers;
    private static final Logger logger = LoggerFactory.getLogger(RulesRecommend.class);

    public double calculateSimilarity(String text1,String text2) throws TikaException, IOException {
        shortWord a1 = TextUtil.getShortWord(text1);
        shortWord a2 = TextUtil.getShortWord(text2);
        CompareTask task = new CompareTask(a1, a2);
        task.execute();
        return task.getSimilarity();
    }


    public RulesRecommend(List<Student> students, List<Teacher> teachers) {
        this.students = students;
        this.teachers = teachers;
    }

//    给学生推荐导师
    public  Map<Long,Teacher>  recommendTeacher(Student student,double departmentSet,double searchWaySet, double hobbySet) throws TikaException, IOException {
        Map<Long,Teacher> finalRecommend = new HashMap<>();
        Map<Teacher,Double> recommendList = new HashMap<>();
        for (Teacher teacher : teachers){
            double score = 0.0;
            String teacherDepartment = teacher.getDepartment().replace("学院","");
            String studentDepartment = student.getDepartment().replace("学院","");
            double similarity = calculateSimilarity(studentDepartment,teacherDepartment);
            if (similarity>0.5) score += similarity;
            else if (similarity>0) score += 0.5;
            else score += 0;
            score = score*departmentSet;

            String[] studentSearchWay = student.getSearchWay().split(";");
            String[] teacherSearchWay = teacher.getSearchWay().split(";");
            int intersetCount = getIntersectionCount(studentSearchWay,teacherSearchWay);
            score += (double) intersetCount /Math.sqrt(studentSearchWay.length*teacherSearchWay.length) * searchWaySet ;

            int hobbyCount = getIntersectionCount(student.getHobby().split(";"),teacher.getHobby().split(";"));
            score += (double) hobbyCount /Math.sqrt(student.getHobby().split(";").length*teacher.getHobby().split(";").length) * hobbySet  ;

            teacher.setSimilarity(score);

            recommendList.put(teacher,score);
        }
        List<Map.Entry<Teacher, Double>> entries = new ArrayList<>(recommendList.entrySet());
        // 根据值进行降序排序
        entries.sort((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()));

        // 输出排序后的结果
        for (Map.Entry<Teacher, Double> entry : entries) {
            entry.getKey().setSimilarity(entry.getValue());
            finalRecommend.put(entry.getKey().getId(),entry.getKey());
        }

        return finalRecommend;
    }

    public  Map<Long,Student> recommendStudent(Teacher teacher,double departmentSet,double searchWaySet, double hobbySet) throws TikaException, IOException {
        Map<Long,Student> finalRecommend = new HashMap<>();
        Map<Student,Double> recommendList = new HashMap<>();
        for (Student student : students){
            double score = 0.0;
            String teacherDepartment = teacher.getDepartment().replace("学院","");
            String studentDepartment = student.getDepartment().replace("学院","");
            double similarity = calculateSimilarity(studentDepartment,teacherDepartment);
            if (similarity>0.5) score += similarity;
            else if (similarity>0) score += 0.5;
            else score += 0;
            score = score*departmentSet;
            String[] studentSearchWay = student.getSearchWay().split(";");
            String[] teacherSearchWay = teacher.getSearchWay().split(";");
            int intersetCount = getIntersectionCount(studentSearchWay,teacherSearchWay);
            score += (double) intersetCount /Math.sqrt(studentSearchWay.length*teacherSearchWay.length) * searchWaySet ;

            int hobbyCount = getIntersectionCount(student.getHobby().split(";"),teacher.getHobby().split(";"));
            score += (double) hobbyCount /Math.sqrt(student.getHobby().split(";").length*teacher.getHobby().split(";").length) * hobbySet ;



            recommendList.put(student,score);
        }
        List<Map.Entry<Student, Double>> entries = new ArrayList<>(recommendList.entrySet());
        // 根据值进行降序排序
        entries.sort((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()));

        // 输出排序后的结果
        for (Map.Entry<Student, Double> entry : entries) {
            entry.getKey().setSimilarity(entry.getValue());
            finalRecommend.put(entry.getKey().getId(), entry.getKey());
        }

        return finalRecommend;

    }
    public static int getIntersectionCount(String[] array1, String[] array2) {
        Set<String> set1 = new HashSet<>(Arrays.asList(array1));
        Set<String> set2 = new HashSet<>(Arrays.asList(array2));

        // 保留set1和set2的交集
        set1.retainAll(set2);

        // 返回交集的大小
        return set1.size();
    }
}
